﻿package Welsy4.Connect.commands
{
	import com.adobe.cairngorm.commands.SequenceCommand;
	import mx.rpc.events.ResultEvent;
	import Welsy4.Connect.business.AuthenticationDelegate;
	import Welsy4.Connect.events.LoginEvent;
	import Welsy4.Connect.events.ChainEvent;
	import Welsy4.Connect.events.ResponseEvent;
	import Welsy4.Connect.model.ConnectModel;
	import Welsy4.Connect.vo.HttpServiceHandler;
	import Welsy4.Connect.vo.User;
	
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;

	public class LogoutCommand extends SequenceCommand implements Command, IResponder
	{
		public function LogoutCommand()
		{
		}

		override public function execute(event:CairngormEvent):void
		{
			ConnectModel.getInstance().addQueue("LogoutCommand.execute");
			this.nextEvent = ChainEvent(event).nextChainedEvent;
			
			var delegate : AuthenticationDelegate = new AuthenticationDelegate(this);
			delegate.Logout();			
			
		}
		
		public function result(event : Object):void
		{
			// response XML
			ConnectModel.getInstance().addQueue("LogoutCommand.result");
			ConnectModel.getInstance().isLogged = false;
			this.executeNextCommand();
			
		}
		
		public function fault(event : Object):void
		{
			ConnectModel.getInstance().addQueue("LogoutCommand.fault");
			CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR,false,ResponseEvent.LOGOUT_COMMAND_FAULT, ResponseEvent.GENERIC_COMMAND_FAULT));
			
		}
		
	}
}